﻿#include "BaseCodeEditor.h"
#include "CodeStyleDef.h"


#if USE_QSCI
BaseCodeEditor::BaseCodeEditor(QWidget *parent)
    : QsciScintilla{parent}
#else
BaseCodeEditor::BaseCodeEditor(QWidget *parent)
    : QTextEdit{parent}
#endif
{
    setFont(CodeStyle::codeBaseFont());
    setContextMenuPolicy(Qt::CustomContextMenu);

#if USE_QSCI
    setMarginsBackgroundColor(Qt::lightGray);
    setMarginType(0, QsciScintilla::NumberMargin);
    setMarginLineNumbers(0, true);
    setMarginWidth(0, 30);   // 40像素宽度，可根据需要调整

    // 启用自动补全
    setAutoCompletionThreshold(1);     // 输入 1 个字符就触发
    setAutoCompletionSource(QsciScintilla::AcsAll);
    setAutoCompletionCaseSensitivity(false);

    setBraceMatching(QsciScintilla::SloppyBraceMatch);  // 括号匹配
    setAutoIndent(true);                                // 自动缩进
    setIndentationsUseTabs(true);
    setIndentationWidth(4);   // 每次缩进是 4 个空格
    setTabWidth(4);
    setCaretLineVisible(true);                          // 高亮当前行
    setCaretLineBackgroundColor(QColor(0xF0, 0xF0, 0xF0));
    setFolding(QsciScintilla::BoxedTreeFoldStyle);      // 代码折叠
#endif
}

QString BaseCodeEditor::code() const
{
#if USE_QSCI
    return text();
#else
    return toPlainText();
#endif
}

void BaseCodeEditor::setLexer(QsciLexer *lexer)
{
#if USE_QSCI
    QsciScintilla::setLexer(lexer);
#else
    ;
#endif
}
